In this example, we will show you how to load a text file into a string variable in C#. For this purpose, we are going to use the StreamReader.ReadToEnd
method to read from a text file on your local machine, or on a network folder.
First, you will need to add the namespace below:
using System.IO;
And the main code:
string str; using (var streamReader = new StreamReader(@"c:\test.txt", Encoding.UTF8)) { str = streamReader.ReadToEnd(); }
Happy coding!
Related Article
Convert HTML table to JSON string in ASP.NET using C# and JavaScript
Check if a Year is Leap Year or Not using C#
How to Swap Two Numbers using C#
Comments